<template>
  <div class="app-container">

    <div id="divSearch" ref="divSearch">
      <el-form :inline="true" :model="Search" class="demo-form-inline">

        <el-form-item label="主题" prop="Search">
          <el-input v-model.trim="Search.Theme" placeholder="主题" />
        </el-form-item>

        <el-form-item>

          <el-button type="primary" icon="el-icon-search" size="small" @click="getDataList">查询</el-button>
          <el-button type="success" icon="el-icon-circle-plus" size="small" @click="handeAdd">新增</el-button>

        </el-form-item>
      </el-form>
    </div>

    <el-table :data="queueList" style="width: 100%; " border>

      <el-table-column type="index" width="50" label="序号">
      </el-table-column>
      <el-table-column align="center" label="主题">
        <template slot-scope="scope">
          {{ scope.row.NoticeTitle }}
        </template>
      </el-table-column>
      <!-- <el-table-column align="header-center" label="内容">
            <template slot-scope="scope">
              {{ scope.row.Contents }}
            </template>
          </el-table-column> -->

      <el-table-column align="header-center" label="失效时间">
        <template slot-scope="scope">
          {{ scope.row.LoseDate }}
        </template>
      </el-table-column>
      <el-table-column align="header-center" label="附件">
        <template slot-scope="scope">
          <v-Getupload :url="'/Common/UploadFile'" :rno="b_Notice" :bill="scope.row.NoticeID"></v-Getupload>
        </template>
      </el-table-column>

      <el-table-column align="header-center" label="创建人" width="100">
        <template slot-scope="scope">
          {{ scope.row.CreateMan }}
        </template>
      </el-table-column>
      <el-table-column align="header-center" label="修改人" width="100">
        <template slot-scope="scope">
          {{ scope.row.Auditer }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="watch(scope)">内容</el-button>
          <el-button type="primary" size="small" @click="handleEdit(scope)">修改</el-button>
          <el-button type="danger" size="small" @click="handleDelete(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination background layout="sizes, prev, pager, next,total" :page-size="Search.pageSize" :total="Search.total"
      @current-change="pageChange" style="float: right;" />

    <el-dialog :visible.sync="dialogVisible" :title="dialogType==='edit'?'编辑':'创建'" width="50%" top="20px">
      <el-form :model="EditObj" label-width="120px" label-position="left">
        <el-form-item label="主题">
          <el-input v-model="EditObj.NoticeTitle" placeholder=" 主题" />
        </el-form-item>
        <el-form-item label="内容">
          <el-input type="textarea" rows="15" v-model="EditObj.NoticeText" placeholder=" 内容" />
        </el-form-item>
        <el-form-item label="失效时间">
          <el-date-picker v-model="EditObj.LoseDate" type="date" placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div style="text-align:right;">
        <el-button type="primary" @click="confirmData">提交</el-button>
        <el-button type="danger" @click="dialogVisible=false">取消</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogVisible2" :title="dialogType==='edit'?'编辑':'内容'" width="50%" top="20px">
      <el-form :model="EditObj" label-width="120px" label-position="left">

        <el-form-item label="内容">
          <el-input type="textarea" rows="15" v-model="EditObj.Contents" placeholder=" 内容" />
        </el-form-item>

      </el-form>
      <div style="text-align:right;">

        <el-button type="danger" @click="dialogVisible2=false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import path from 'path'
  import {
    deepClone
  } from '@/utils'
  import {
    getSelectList,
    updateNotice,
    deleteNotice
  } from '@/api/getSelect'
  import vGetupload from "../Common/Getupload";
  // 默认的编辑字段
  const defaultObject = {

  }


  export default {
    name: "QueueList",
    components: {
      vGetupload
    },
    data() {
      return {
        EditObj: Object.assign({}, defaultObject),
        queueList: [], //队列的列表 在el-table中用
        dialogVisible: false,
        dialogVisible2: false,
        Search: { // 查询条件
          total: 0, // 总记录数
          pageSize: 10, // 一页显示的记录数
          page: 1, // /当前页码
          RNO: '005', // 表编号  根据Controller中对应的表编号
          jsWhere: '' // 自定义条件
        }
      }
    },
    computed: {
      routesData() {
        return this.routes
      }
    },
    created() {
      // Mock: get all routes and roles list from server

      this.getDataList()
    },
    methods: {
      pageChange(page) { // 翻页
        this.Search.page = page

        this.getDataList()
      },
      async getDataList() { // 获取列表数据，通用

        const res = await getSelectList({
          data: JSON.stringify(this.Search)
        })

        this.queueList = res.data.Table //将data 中的数据放到queueList中
        this.Search.total = res.data.Table1[0].RecordCount
        
      },
      sortChange: function(column) {
        // column, prop, order,排序，通用
        
        this.Search.sort = column.prop + ' ' + column.order

        this.getDataList()
      },
      handeAdd() { // 新建
        this.EditObj = Object.assign({}, defaultObject)
        if (this.$refs.tree) {
          this.$refs.tree.setCheckedNodes([])
        }
        this.dialogType = 'new'
        this.dialogVisible = true

      },
      watch(scope) {
        this.dialogVisible2 = true
        this.EditObj = deepClone(scope.row)
      },
      handleEdit(scope) { // 修改

        this.dialogType = 'edit'
        this.dialogVisible = true
        this.checkStrictly = true
        this.EditObj = deepClone(scope.row)


        this.$nextTick(() => {
          const routes = this.generateRoutes(this.EditObj.routes)
          this.$refs.tree.setCheckedNodes(this.generateArr(routes))
          // set checked state of a node not affects its father and child nodes
          this.checkStrictly = false
        })

      },
      async handleDelete({
        $index,
        row
      }) {
        this.$confirm('确定删除该记录吗?', '警告', {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning'
          })
          .then(async () => {
            await deleteNotice({
              ID: JSON.stringify(row.ID)
            })
            this.queueList.splice($index, 1)
            if (result.code === 20000) {
              this.dialogVisible = false
              this.$notify({
                title: 'Success',
                dangerouslyUseHTMLString: true,
                message: result.message,
                type: 'success'
              })
            } else {
              this.$notify({
                title: 'error',
                dangerouslyUseHTMLString: true,
                message: result.message,
                type: 'error'
              })
            }

          })

      },
      async confirmData() { // 提交数据，需要修改
        const isEdit = this.dialogType === 'edit'

        if (isEdit || true) {
          const result = await updateNotice({
            head: JSON.stringify(this.EditObj),
            billID: this.EditObj.ID,
            IsPostBack: true
          })

          

          if (result.code === 20000) {
            const {
              description,
              key,
              name
            } = this.EditObj
            this.dialogVisible = false
            this.$notify({
              title: 'Success',
              dangerouslyUseHTMLString: true,
              message: result.message,
              type: 'success'
            })
            this.getDataList()

          } else {
            this.$notify({
              title: 'error',
              dangerouslyUseHTMLString: true,
              message: result.message,
              type: 'error'
            })
          }
        }
      }

    }
  }
</script>

<style lang="scss" scoped>
  .app-container {
    .EditObjs-table {
      margin-top: 30px;
    }

    .permission-tree {
      margin-bottom: 5px;
    }

    .el-textarea__inner {
      min-height: 500px;
    }
  }
</style>
